//
//  SGFGtasksViewController.m
//  Estate
//
//  Created by heyong on 16/5/26.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "SGFGtasksViewController.h"

@interface SGFGtasksViewController ()<UIScrollViewDelegate>

@end

@implementation SGFGtasksViewController
-(UIScrollView *)contentScrollview{
    if(_contentScrollview==nil){
        _contentScrollview=[[UIScrollView alloc]init];
    }
    return _contentScrollview;
}
- (UISegmentedControl *)headerSegment{
    if(_headerSegment==nil){
        NSArray *arr = [[NSArray alloc]initWithObjects:@"待办",@"历史",nil];
        _headerSegment=[[UISegmentedControl alloc]initWithItems:arr];
        _headerSegment.frame=CGRectMake(10, 74, DEF_SCREEN_WIDTH-20, 35*BIZ);
    }
    return _headerSegment;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //添加滚动视图
    [self.view addSubview:self.contentScrollview];
    //添加选择器
    [self.view addSubview:self.headerSegment];
    //创建视图样式
    [self createView];
}
#pragma mark -创建视图
- (void) createView{
    self.title=@"智慧监理";
    self.view.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    [self.contentScrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerSegment.mas_bottom).offset(10);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.offset(667*BIZ);
    }];
    //默认选中
    self.headerSegment.selectedSegmentIndex = 0;
    [_headerSegment setTintColor:[TTTheme defaultManger].defaultColor];
    // 未选中颜色
    [_headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName:[TTTheme defaultManger].selectColor,NSFontAttributeName:[UIFont systemFontOfSize:17.*BIZ]} forState:UIControlStateNormal];
    //选中颜色 字体大小
    [_headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:17.*BIZ]} forState:UIControlStateSelected];
    
    //设置滚动视图
    [self setUpScrollView];
    //设置子视图
    [self setUpChildViewControll];
    //为选择器添加事件
    [_headerSegment addTarget:self action:@selector(segmentSelect:) forControlEvents:UIControlEventValueChanged];
}

//点击选择器事件
-(void)segmentSelect:(UISegmentedControl*)seg{
    NSInteger index = seg.selectedSegmentIndex;
    NSLog(@"index：%li",(long)index);
    CGRect frame = _contentScrollview.frame;
    frame.origin.x = index * CGRectGetWidth(_contentScrollview.frame);
    frame.origin.y = 0;
    [_contentScrollview scrollRectToVisible:frame animated:YES];
}

//设置滚动视图属性
-(void)setUpScrollView{
    _contentScrollview.pagingEnabled = YES;
    _contentScrollview.bounces = NO;
    _contentScrollview.delegate = self;
    _contentScrollview.showsHorizontalScrollIndicator = NO;
    // 方向锁
    _contentScrollview.directionalLockEnabled = YES;
    //取消自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    _contentScrollview.contentSize = CGSizeMake(375 * 2 * BIZ, DEF_SCREEN_HEIGHT);
}
/**
 *  设置控制的每一个子控制器
 */
-(void)setUpChildViewControll{
    //初始化视图控制器
    self.sgfdbCtr = [[GSGFDBViewController alloc]init];
    self.sgflsCtr=[[GSGFLSViewController alloc]init];
    
    //指定该控制器为其子控制器
    [self addChildViewController:_sgfdbCtr];
    [self addChildViewController:_sgflsCtr];
  
    //将视图view加入到scrollview上
    [_contentScrollview addSubview:_sgfdbCtr.view];
    [_contentScrollview addSubview:_sgflsCtr.view];
    
    //!!!设置控制器的位置
    //待办
    CGRect dbCtrRect = _sgfdbCtr.view.frame;
    dbCtrRect.origin.x = 0;
    dbCtrRect.size.height =DEF_SCREEN_HEIGHT;
    _sgfdbCtr.view.frame = dbCtrRect;
    
    //历史
    CGRect lsCtrRect = _sgflsCtr.view.frame;
    lsCtrRect.origin.x = DEF_SCREEN_WIDTH;
    lsCtrRect.size.height = CGRectGetHeight(_contentScrollview.frame);
    _sgflsCtr.view.frame = lsCtrRect;
    
}
#pragma mark -UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSetX = scrollView.contentOffset.x;
    NSInteger ratio = round(offSetX / DEF_SCREEN_WIDTH);
    _headerSegment.selectedSegmentIndex = ratio;
}

@end
